/**
 * 
 */
package com.ej.methods;

import java.util.Date;

/**
 * MAKE DEFENSIVE COPIES WHEN NEEDED
 * 
 * @author Sunaina.Sharma
 * 
 */
public class DefensiveCopies {

	// Broken "immutable" time period class
	public final class Period {
		private final Date start;
		private final Date end;

		/**
		 * @param start
		 *            the beginning of the period
		 * @param end
		 *            the end of the period; must not precede start
		 * @throws IllegalArgumentException
		 *             if start is after end
		 * @throws NullPointerException
		 *             if start or end is null
		 */
		public Period(Date start, Date end) {

			// This will restrict user from modifying date as compared to
			// this.start = start;
			// this.end = end;

			this.start = new Date(start.getTime());
			this.end = new Date(end.getTime());
			if (this.start.compareTo(this.end) > 0)
				throw new IllegalArgumentException(start + " after " + end);
		}

		/**
		 * @return
		 */
		public Date start() {
			// modify the accessors to return defensive copies of mutable
			// internal fields.Use this as compared to return start;
			return new Date(start.getTime());
		}

		/**
		 * @return
		 */
		public Date end() {
			// Use this as compared to return end;
			return new Date(end.getTime());
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
